package com.zyt.springboot.numberToChinese;

public class NumberToChinese {
    private static final String[] DIGITS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] SECTION_UNITS = {"", "万", "亿", "兆", "京"};

    public static void main(String[] args) {
        int number = 292103194;
        System.out.println(toChinese(number));
    }

    public static String toChinese(int number) {
        if (number == 0) {
            return DIGITS[0];
        }

        String numStr = Integer.toString(number);
        StringBuilder reversed = new StringBuilder(new StringBuilder(numStr).reverse().toString());
        int length = reversed.length();
        int paddedLength = (length % 4 == 0) ? length : length + 4 - length % 4;
        while (reversed.length() < paddedLength) {
            reversed.append('0');
        }

        int sectionCount = paddedLength / 4;
        String[] sections = new String[sectionCount];
        for (int i = 0; i < sectionCount; i++) {
            String section = reversed.substring(i * 4, (i + 1) * 4);
            sections[i] = new StringBuilder(section).reverse().toString();
        }

        StringBuilder result = new StringBuilder();
        for (int i = sectionCount - 1; i >= 0; i--) {
            String section = sections[i];
            String converted = convertSection(section);
            if (converted.isEmpty()) {
                continue;
            }

            boolean isHighestSection = (i == sectionCount - 1);
            if (!isHighestSection) {
                boolean isLeadingZero = section.charAt(0) == '0';
                if (isLeadingZero) {
                    if (result.length() > 0 && result.charAt(result.length() - 1) != '零') {
                        result.append("零");
                    }
                }
            }

            result.append(converted).append(SECTION_UNITS[i]);
        }

        String finalResult = result.toString();
        // 处理连续的零
        finalResult = finalResult.replaceAll("零+", "零");
        // 去除末尾的零
        if (finalResult.endsWith("零")) {
            finalResult = finalResult.substring(0, finalResult.length() - 1);
        }
        return finalResult;
    }

    private static String convertSection(String section) {
        StringBuilder sb = new StringBuilder();
        boolean prevZero = false;
        boolean hasOutput = false;
        for (int i = 0; i < 4; i++) {
            char c = section.charAt(i);
            int num = c - '0';
            if (num != 0) {
                if (prevZero && hasOutput) {
                    sb.append(DIGITS[0]);
                    prevZero = false;
                }
                sb.append(DIGITS[num]);
                switch (i) {
                    case 0:
                        sb.append("仟");
                        break;
                    case 1:
                        sb.append("佰");
                        break;
                    case 2:
                        sb.append("拾");
                        break;
                }
                hasOutput = true;
            } else {
                prevZero = true;
            }
        }
        return sb.toString();
    }
}
